package dianmobile.eistarandroid.ui.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import dianmobile.eistarandroid.ByhhAndroidApplication;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.UserInfo;
import dianmobile.eistarandroid.network.request.RequestCallback;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.network.request.UserDetailRequest;
import dianmobile.eistarandroid.ui.views.SwipeActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.DataUtils;

public class UserInfoActivity extends SwipeActivity {

    private String userName;

    private ImageButton btnBack;
    private ImageButton btnRight;
    private ImageView ivUserHead;
    private TextView tvUserName;
    private ImageView ivUserGender;
    private TextView tvUserHoroscope;
    private TextView tvUserDuty;

    private TextView tvNickName;
    private TextView tvPostNum;
    private TextView tvNetAge;
    private TextView tvLife;
    private TextView tvMedals;
    private TextView tvMoney;
    private TextView tvExp;
    private TextView tvLoginNum;
    private TextView tvPlans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ByhhAndroidApplication application = (ByhhAndroidApplication)getApplication();
        application.getActivityList().add(this);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_user_info);

        //获取启动该Activity的用户名
        Intent intent = getIntent();
        userName = intent.getStringExtra(ConstantsData.EXTRA_USER_NAME);

        initView();


        if(userName.equals(application.getLoginUserName())) {
            btnRight.setBackgroundResource(R.drawable.ic_logout);
        }

        if (userName != null) {
            getUserInfoFromNewWort(userName);
        }

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfoActivity.this.finish();
            }
        });
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 判断userName是否和登录名相同
                if(userName.equals(application.getLoginUserName())) {
                    //TODO clear cookies
                    Intent intent = new Intent(UserInfoActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("isForceToWaitUserInput", true);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(UserInfoActivity.this, PostArticleActivity.class);
                    intent.putExtra(ConstantsData.EXTRA_POST_TYPE, ConstantsData.POST_TYPE_MAIL);
                    intent.putExtra(ConstantsData.EXTRA_USER_NAME, userName);
                    startActivity(intent);
                }


            }
        });
    }

    @Override
    public void onDestroy() {
        ByhhAndroidApplication application = (ByhhAndroidApplication)getApplication();
        application.getActivityList().remove(this);
        super.onDestroy();
    }

    private void initView(){
        btnBack = (ImageButton)findViewById(R.id.btn_user_info_back);
        btnRight = (ImageButton)findViewById(R.id.btn_user_info_right);
        ivUserHead = (ImageView)findViewById(R.id.iv_user_info_header);
        tvUserName = (TextView)findViewById(R.id.tv_user_info_name);
        ivUserGender = (ImageView)findViewById(R.id.iv_user_info_gender);
        tvUserHoroscope = (TextView)findViewById(R.id.tv_user_info_horoscope);
        tvUserDuty = (TextView)findViewById(R.id.tv_user_info_duty);

        tvNickName = (TextView)findViewById(R.id.tv_user_info_nickname);
        tvPostNum = (TextView)findViewById(R.id.tv_user_info_postNum);
        tvNetAge = (TextView)findViewById(R.id.tv_user_info_netAge);
        tvLife = (TextView)findViewById(R.id.tv_user_info_life);
        tvMedals = (TextView)findViewById(R.id.tv_user_info_medals);
        tvMoney = (TextView)findViewById(R.id.tv_user_info_money);
        tvExp = (TextView)findViewById(R.id.tv_user_info_exp);
        tvLoginNum = (TextView)findViewById(R.id.tv_user_info_loginNum);
        tvPlans = (TextView)findViewById(R.id.tv_user_info_plans);

    }

    private void setView(UserInfo userInfo) {
        RequestFactory.makeNetworkImage(ivUserHead, DataUtils.getUserHeadUrlFromName(userName),
                R.drawable.default_face, 0);
        tvUserName.setText(userName);
        if (userInfo.getGender() != null && userInfo.getGender().equals("M")) {//male
            ivUserGender.setImageResource(R.drawable.ic_arrow_back);
        } else if (userInfo.getGender() != null && userInfo.getGender().equals("F")) {//female
            ivUserGender.setImageResource(R.drawable.ic_more_gray);
        } else {
            //保密
        }

        tvUserHoroscope.setText(userInfo.getHoroscope());
        tvUserDuty.setText(userInfo.getDuty());
        tvNickName.setText(userInfo.getNickName());
        tvPostNum.setText(removeNull(userInfo.getNumPosts(), userInfo.getStrPosts()));
        tvNetAge.setText(removeNull(userInfo.getNetAge(), userInfo.getStrNetage()));
        tvLife.setText(userInfo.getLife());
        tvMedals.setText(removeNull(userInfo.getMedals(), userInfo.getStrMedals()));
        tvMoney.setText(removeNull(userInfo.getMoney(), userInfo.getStrMoney()));
        tvExp.setText(removeNull(userInfo.getExp(),  userInfo.getStrExp()));
        tvLoginNum.setText(removeNull(userInfo.getNumLogins(), userInfo.getLastLogin()));
        tvPlans.setText(userInfo.getPlans());
    }

    private String removeNull(String str1, String str2){
        if (str2 == null || str2.isEmpty()){
            return str1;
        }else {
            return str1 + " (" + str2 + ")";
        }
    }

    private void getUserInfoFromNewWort(String userName) {
        Map<String, String> params = new HashMap<>();
        params.put(ConstantsData.EXTRA_USER_NAME, userName);

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("加载中...");
        progressDialog.show();

        UserDetailRequest.execute(this, params, new RequestCallback() {
            @Override
            public void onResult(Map<String, Object> result, String error) {
                if (error == null) {
                    UserInfo userInfo = (UserInfo)result.get(ConstantsData.USER_INFO);
                    setView(userInfo);
                    progressDialog.dismiss();
                } else {
                    progressDialog.dismiss();
                    Toast.makeText(UserInfoActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
